package cn.calm.swing.osgi.file.New.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.BevelBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

import cn.calm.swing.osgi.api.WizardItemService;
import cn.calm.swing.osgi.util.SwingHelper;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class WizardDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane1;
	private JTree jTree1;
	private JButton cancel;
	private JButton next;
	private JPanel jPanel3;
	private JPanel jPanel2;
	private BundleContext context;
	private DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");

	public WizardDialog(BundleContext context) {
		this.context = context;
		setTitle("New");
		this.setSize(700, 450);
		SwingHelper.center(this);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		BorderLayout thisLayout = new BorderLayout();
		getInsets();
		getContentPane().setLayout(thisLayout);
		this.setModal(true);
		{
			final JPanel jPanel1 = new JPanel();
			getContentPane().add(jPanel1, BorderLayout.NORTH);
			jPanel1.setPreferredSize(new java.awt.Dimension(392, 60));
		}
		{
			jPanel3 = new JPanel();
			BorderLayout jPanel3Layout = new BorderLayout();
			jPanel3.setLayout(jPanel3Layout);
			getContentPane().add(jPanel3, BorderLayout.CENTER);
			{
				jScrollPane1 = new JScrollPane();
				jPanel3.add(jScrollPane1, BorderLayout.CENTER);
				{

					jTree1 = new JTree(root);
					jTree1.addMouseListener(new MouseAdapter() {

						@Override
						public void mouseClicked(MouseEvent e) {
							if (e.getClickCount() == 2) {
								fireAction();
							}
						}
					});

					jTree1.addTreeSelectionListener(new TreeSelectionListener() {

						@Override
						public void valueChanged(TreeSelectionEvent e) {
							TreeNode node = (TreeNode) e.getPath()
									.getLastPathComponent();
							if (node.isLeaf()) {
								next.setEnabled(true);
							} else {
								next.setEnabled(false);
							}
						}
					});
					jTree1.setRootVisible(false);
					jTree1.setCellRenderer(new IconDefaultTreeCellRenderer());
					jScrollPane1.setViewportView(jTree1);
					jTree1.setBorder(BorderFactory
							.createBevelBorder(BevelBorder.LOWERED));
					jScrollPane1.setBorder(BorderFactory.createEmptyBorder(10,
							10, 10, 10));
				}
			}
		}
		{
			jPanel2 = new JPanel();
			getContentPane().add(jPanel2, BorderLayout.SOUTH);
			FlowLayout jPanel2Layout = new FlowLayout();
			jPanel2Layout.setAlignment(FlowLayout.RIGHT);
			jPanel2.setLayout(jPanel2Layout);
			{
				next = new JButton();
				jPanel2.add(next);
				next.setEnabled(false);
				next.setText("\u4e0b\u4e00\u6b65");
				next.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						fireAction();
					}
				});
			}
			{
				cancel = new JButton();
				jPanel2.add(cancel);
				cancel.setText("\u53d6\u6d88");
				cancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						WizardDialog.this.dispose();
					}
				});
			}
		}
	}

	public static void main(String[] args) {
		new WizardDialog(null).setVisible(true);
	}

	private void fireAction() {
		TreePath path = jTree1.getSelectionPath();
		if (path == null) {
			return;
		}
		TreeNode node = (TreeNode) path.getLastPathComponent();
		if (!node.isLeaf()) {
			return;
		}
		String tree = path.toString().replace(", ", "/").replace("[", "")
				.replace("]", "");
		int index = tree.indexOf("/");
		tree = tree.substring(index + 1);
		try {
			Collection<ServiceReference<WizardItemService>> serviceReferences = context
					.getServiceReferences(WizardItemService.class, "("
							+ WizardItemService.WIZARD_ITEM_TREE + "=" + tree
							+ ")");
			if (serviceReferences.isEmpty()) {
				return;
			}
			final Iterator<ServiceReference<WizardItemService>> iterator = serviceReferences
					.iterator();
			if (iterator.hasNext()) {
				dispose();
				WizardItemService service = context.getService(iterator.next());
				service.action();
			}
		} catch (InvalidSyntaxException e) {
			e.printStackTrace();
		}
	}

	public JTree getTree() {
		return jTree1;
	}

	public DefaultMutableTreeNode getRoot() {
		return root;
	}
}
